<template>
  <div>
    <el-container>
      <el-header height="70px">
        <p class="header_title">轮播图管理</p>
        <!--  -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>轮播图维护</el-breadcrumb-item>
          <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
          <el-breadcrumb-item></el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>

      <el-main>
        <el-card class="box-card">
          <div slot="header">
            <span>轮播图列表</span>
            <span style="float: right;">
              <i class="el-icon-arrow-up"></i>
              <i class="el-icon-close"></i>
            </span>
          </div>

          <el-row :gutter="20">
            <!-- 搜索框 -->
            <el-col :span="8">
              <el-input  placeholder="根据名称查询" v-model="pagelist.name" clearable>
                <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
              </el-input>
            </el-col>
            <!-- 添加轮播图 -->
            <el-col :span="4">
              <el-button
                type="primary"
                class="add"
                @click="addDialogVisible = true"
                >添加</el-button
              >
            </el-col>
             <el-col :span="5">
                <b class="sum_data">数据总数：</b>
               <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.pagenum"
                :page-size="pagination.pagesize"
                 :page-sizes="[3, 5, 10, 20]"
                layout="total, sizes"
                :total="pagination.total"
                class="top_nav"
                >
               </el-pagination>
            </el-col>
            
          </el-row>

          <!-- 表格 -->
          <el-table :data="pagelist" style="width: 100%" border stripe>
            <el-table-column prop="name" label="名称" width="160px">
            </el-table-column>
            <el-table-column prop="image" label="图片" width="200px">
              <template slot-scope="scope">
                <el-image
                  :src="scope.row.image"
                  style="width:50px;height:50px"
                  fit="cover"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column prop="location" label="图片位置" width="80px">
            </el-table-column>
            <el-table-column prop="type" label="跳转类型" width="120px">
            </el-table-column>
            <el-table-column prop="target" label="跳转目标" width="120px">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="danger" size="mini" @click="removeId(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.pagenum"
            :page-size="pagination.pagesize"
            background
            layout="prev, pager, next,total"
            :total="pagination.total"
          >
          </el-pagination>
        </el-card>
      </el-main>
    </el-container>

    <!-- 添加对话框 -->
    <!--  @close="addDialogClosed" -->
    <el-dialog
      title="添加轮播图"
      :visible.sync="addDialogVisible"
      width="60%"
     
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="addForm.name"
                placeholder="请输入名称"
                style="width:160px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="图片位置" prop="location">
              <el-select
                v-model="addForm.location"
                placeholder="选择图片位置"
                style="width: 100%"
              >
                <el-option
                  v-for="item in picPlace"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="跳转类型" prop="type">
              <el-select
                v-model="addForm.type"
                placeholder="选择图片位置"
                style="width: 100%"
              >
                <el-option
                  v-for="item in typePlace"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="跳转目标" prop="target">
              <el-select
                v-model="addForm.target"
                placeholder="选择跳转目标"
                style="width: 100%"
              >
                <el-option
                  v-for="item in targetPlace"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="是否置顶" prop="topping">
              <el-switch
                v-model="addForm.topping"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示" prop="display">
              <el-switch
                v-model="addForm.display"
                active-color="#13ce66"
                inactive-color="#ff4949"
                 active-value="1"
                inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="置顶排序" prop="sequence">
              <el-input
                v-model="addForm.sequence"
                placeholder="请输入顺序"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item prop="file">
          <div class="pictitle">
            <b>上传头像</b>(请上传文件大小不超过1MB的正方形图片)
          </div>

          <el-upload
            class="avatar-uploader"
            ref="upload"
            action="#"
            :data="addForm"
            :show-file-list="false"
            :multiple="false"
            :auto-upload="false"
            :on-change="imgSaveToUrl"
          >
            <img v-if="this.addForm.image" :src="this.addForm.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
     

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addChart">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInfo: {
        total: 1,
        charts: "",
      },
      pagination: {
        pagenum: 1,
        pagesize: 3,
        total: 5,
      },
      pagelist: [],
      //
      imageUrl: "",
      addDialogVisible: false,
      value: true,
      // 添加轮播图表单数据
      addForm: {
        name: "",
        type: " ",
        location: "",
        target: "",
        sequence: "",
        topping: "",
        display:"",
        image:"",
      },
      file:"",

      // 添加表单的校验
      addFormRules: {
        name: [{ required: true, message: "请输入名称", trigger: "change" }],
        location: [
          { required: true, message: "请输入图片路径", trigger: "change" },
        ],
        type: [
          { required: true, message: "请选择跳转类型", trigger: "change" },
        ],
        target: [
          { required: true, message: "请选择跳转路径", trigger: "change" },
        ],
        sequence: [
          { required: true, message: "请选择数字", trigger: "change" },
        ],
      },
      picPlace: [
        { value: "首页", label: "首页" },
        { value: "资讯", label: "资讯" },
      ],
      typePlace: [
        { value: "商品", label: "商品" },
        { value: "资讯", label: "资讯" },
      ],
      targetPlace: [
        { value: "商品详细", label: "商品详细" },
        { value: "资讯详细", label: "资讯详细" },
      ],
    };
  },
  methods: {
    async chartList() {
      const res = await this.http.get("/chart/select", this.pagination);
      this.pagelist = res.charts;
      this.pagination.total = res.total;
    },
    handleSizeChange(newSize) {
      this.pagination.pagesize = newSize;
      this.chartList();
    },
    handleCurrentChange(newPage) {
      this.pagination.pagenum = newPage;
      this.chartList();
    },
    // 图片上传
    async imgSaveToUrl(event) {
      if (this.beforeUploadPicture(event.raw)) {
        var formData = new FormData();
        formData.append("file", event.raw);
        // console.log(formData);
        this.http.upload("/chart/upload", formData).then((res) => {
          if (res.code == 200) {
            this.addForm.image = res.data
            console.log(this.addForm);
            this.$message.success("上传成功");
          }
        });
      }
    },// 图片上传之前
    beforeUploadPicture(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG 或者 PNG 格式!");
      }
      if (isJPG && isPNG) {
        if (!isLt10M) {
          this.$message.error("上传头像图片大小不能超过 10MB!");
        }
      }
      return (isJPG || isPNG) && isLt10M;
    },
    // /chart/add
    addChart() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起添加网络请求
        const { res } = await this.http.post("/chart/add", this.addForm);
        if (status !== 400) {
          this.$message.error("添加行为失败");
        }
        console.log(res);
        this.$message.success("添加行为成功");
        this.addDialogVisible = false;
        this.chartList();
      });
    },
    //
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 删除
    async removeId(row){
       console.log(row);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => 
      
      
      );
      console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const res = await this.http.post("/chart/delete",row.id);
      console.log(res);
      this.chartList();
    },
    async search(){
      const res = await this.http.get('/chart/selectByname',{
        name:this.pagelist.name,
        pagenum:this.pagination.pagenum,
        pagesize:this.pagination.pagesize
      })
      console.log(res);
      this.pagelist = res.list
    }
    
  },
  created() {
    this.chartList();
  },
};
</script>

<style scoped>
.el-form-item {
  display: inline-block !important;
  margin-top: 20px;
}
.el-form-item__label {
  display: none;
  width: 0px !important;
}
.el-form .el-form-item .el-input {
  width: 300px;
  margin-left: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 2px dashed #dedede;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-table-column {
  height: 50px;
}
.pictitle {
  margin-left: -40px;
}
.confirmBtn {
  margin-left: 80px;
}
.el-button + .el-button {
  margin-left: 80px;
}
.el-form-item__label {
  margin-right: 40px;
}
</style>
